package GUI;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Toolkit;
import java.awt.Transparency;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;

import Entita.Tavola;

public class VisualizzaTavola extends JFrame {

	private class TurnButton extends JButton{
		private JFrame finestra;
		public TurnButton(String arg0, JFrame finestra) {
			super(arg0);
			this.finestra=finestra;
		}
		public JFrame getFinestra() {
			return finestra;
		}
		
	}
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JPanel contentPane;
	private JLabel label;
	private ImageIcon immagine;
	private ImageIcon immagineSx;
	private ImageIcon immagineDx;
	private ImageIcon immagineCapovolta;


	private static BufferedImage rotate(BufferedImage image, double angle) {
		double angleRad=angle*Math.PI/180;
	    double sin = Math.abs(Math.sin(angleRad)), cos = Math.abs(Math.cos(angleRad));
	    int w = image.getWidth(), h = image.getHeight();
	    int neww = (int)Math.floor(w*cos+h*sin), newh = (int)Math.floor(h*cos+w*sin);
	    GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
	    GraphicsDevice gd = ge.getDefaultScreenDevice();
	    GraphicsConfiguration gc = gd.getDefaultConfiguration();;
	    BufferedImage result = gc.createCompatibleImage(neww, newh, Transparency.TRANSLUCENT);
	    Graphics2D g = result.createGraphics();
	    g.translate((neww-w)/2, (newh-h)/2);
	    g.rotate(angleRad, w/2, h/2);
	    g.drawRenderedImage(image, null);
	    g.dispose();
	    return result;
	}
	
	/**
	 * Create the frame.
	 */
	public VisualizzaTavola(Tavola tavola) {

		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		this.setTitle("Tavola " + tavola.getNumeroLocalizzazioni());
		immagine=new ImageIcon(tavola.getImagine());
		immagineSx=new ImageIcon(rotate((BufferedImage)tavola.getImagine(), -90));
		immagineDx=new ImageIcon(rotate((BufferedImage)tavola.getImagine(), 90));
		immagineCapovolta=new ImageIcon(rotate((BufferedImage)tavola.getImagine(), 180));
		int maxDimension = Math.max(immagine.getIconHeight(),immagine.getIconWidth());
		contentPane = new JPanel();
		int height=Toolkit.getDefaultToolkit().getScreenSize().height;
		int width=Toolkit.getDefaultToolkit().getScreenSize().width;
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		contentPane.setBackground(Color.WHITE);
		this.setTitle(tavola.toString());
		this.setResizable(false);


		setContentPane(contentPane);
		

		
		
		label=new JLabel(immagine);
		label.setBounds(0, 0, maxDimension, maxDimension);
		contentPane.add(label,BorderLayout.CENTER);
		
		
		JPanel pannelloBottoni = new JPanel();
		contentPane.add(pannelloBottoni, BorderLayout.SOUTH);
		
		TurnButton bottoneSu = new TurnButton("DRITTA",this);
		bottoneSu.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				TurnButton bottone=(TurnButton)e.getSource();
				JFrame finestra=bottone.getFinestra();
				int height=Toolkit.getDefaultToolkit().getScreenSize().height;
				int width=Toolkit.getDefaultToolkit().getScreenSize().width;
				
				label.setIcon(immagine);
				pack();
				finestra.setLocation(width/2-finestra.getWidth()/2, height/2-finestra.getHeight()/2);
			}
		});
		pannelloBottoni.add(bottoneSu);
		pannelloBottoni.setBackground(Color.WHITE);

		TurnButton bottoneGiu = new TurnButton("CAPOVOLTA",this);
		bottoneGiu.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				TurnButton bottone=(TurnButton)e.getSource();
				JFrame finestra=bottone.getFinestra();
				int height=Toolkit.getDefaultToolkit().getScreenSize().height;
				int width=Toolkit.getDefaultToolkit().getScreenSize().width;
				label.setIcon(immagineCapovolta);
				pack();
				finestra.setLocation(width/2-finestra.getWidth()/2, height/2-finestra.getHeight()/2);
			}
		});
		pannelloBottoni.add(bottoneGiu);

		TurnButton bottoneSinistra = new TurnButton("SINISTRA",this);
		bottoneSinistra.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				TurnButton bottone=(TurnButton)e.getSource();
				JFrame finestra=bottone.getFinestra();
				int height=Toolkit.getDefaultToolkit().getScreenSize().height;
				int width=Toolkit.getDefaultToolkit().getScreenSize().width;
				label.setIcon(immagineSx);
				pack();
				finestra.setLocation(width/2-finestra.getWidth()/2, height/2-finestra.getHeight()/2);
			}
		});
		pannelloBottoni.add(bottoneSinistra);

		TurnButton bottoneDestra = new TurnButton("DESTRA",this);
		bottoneDestra.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				TurnButton bottone=(TurnButton)e.getSource();
				JFrame finestra=bottone.getFinestra();
				int height=Toolkit.getDefaultToolkit().getScreenSize().height;
				int width=Toolkit.getDefaultToolkit().getScreenSize().width;
				label.setIcon(immagineDx);
				pack();
				finestra.setLocation(width/2-finestra.getWidth()/2, height/2-finestra.getHeight()/2);
			}
		});
		pannelloBottoni.add(bottoneDestra);

		
		
		pack();
		this.setLocation(width/2-this.getWidth()/2, height/2-this.getHeight()/2);

	}
}
